﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Exam.Models;

namespace Exam.DAL
{
    public class 用户表DAL
    {
        /// <summary>
        /// 通过编号和密码查询数据记录数
        /// </summary>
        /// <param name="编号"></param>
        /// <param name="密码"></param>
        /// <returns></returns>
        public int GetRecordCount(string 编号,string 密码) 
        {
            string sql = "SELECT COUNT(ID) FROM [用户表] WHERE [编号]=@Code AND [密码]=@pwd";
            SqlParameter[] pars = {
                  new SqlParameter("@Code",编号),
                  new SqlParameter("@pwd",密码)
            };
            var count = SQLHelper.ExecuteScalar(CommandType.Text, sql, pars);
            if (count == null) return 0;
            else return Convert.ToInt32(count);
        }
        /// <summary>
        /// 通过编号返回用户
        /// </summary>
        /// <param name="编号"></param>
        /// <returns></returns>
        public List<用户表> GetUser(string 编号)
        {
            List<用户表> l = new List<用户表>() ;
            string sql = "SELECT * FROM [用户表] WHERE [编号]=@Code";
            SqlParameter par = new SqlParameter("@Code", 编号);
            using (SqlDataReader reader=SQLHelper.ExecuteGetReader(CommandType.Text,sql,par))
            {
                while (reader.Read())
                {
                    try
                    {
                        用户表 u = new 用户表();
                        u.ID = Guid.Parse(reader["ID"].ToString());
                        u.编号 = reader["编号"].ToString();
                        u.部门ID = Guid.Parse(reader["部门ID"].ToString());
                        u.角色 = Convert.ToInt32(reader["角色"].ToString());
                        u.密码 = reader["密码"].ToString();
                        u.添加时间 = Convert.ToDateTime(reader["添加时间"]);
                        u.性别 = Convert.ToInt32(reader["性别"]);
                        u.姓名 = reader["姓名"].ToString();
                        u.邮箱 = reader["邮箱"].ToString();
                        l.Add(u);
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                }
                return l;
            }
        }
    }
}
